<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {

        $this->printToyilianyun();
        return $this->view->fetch();
    }

    public function printToyilianyun()
    {
        $orderList = Db::name('shopro_order')->where(['status'=>'paid'])->select();
        foreach($orderList as $order)
        {
            $userId = $order['user_id'];
            $user =  Db::name('user')->where(['id'=> $userId])->find();
            try{
                $templateTxt = "夏胖子果蔬系统 \n 订单编号:".$order['order_sn']." \n 下单时间:".
                date('Y-m-d H:i:s',$order['createtime'])
                ." \n 用户:".$user['nickname']." \n 手机号:".$user['mobile']." \n 订单备注:".$order['remark'];


                $goods = Db::query('select t1.title from fa_shopro_order_item t INNER JOIN fa_shopro_goods t1 on t.goods_id=t1.id
                where order_id=:orderid',['orderid'=> $order['id']]);
                $templateTxt=$templateTxt." \n ---------------";
                foreach($goods as $good)
                {
                    $goodTitle =$good['title'];
                    if(strlen($goodTitle)>5)
                    {
                        $goodTile = substr($goodTitle, 0, 5);
                    }
                 
                    $templateTxt=$templateTxt."\n";
                    $templateTxt=$templateTxt.$goodTitle;
                    $templateTxt=$templateTxt."\n";
                }
                $templateTxt=$templateTxt."-----------";
                $templateTxt= $templateTxt." \n 订单金额:".$order['order_amount'];
             //     echo $templateTxt;
             \addons\uniprint\library\printer::printingTemplate($templateTxt, "3");
             Db::name('shopro_order')->where(['id'=>$order['id']])->update(['status'=>'completed']);
             //    \addons\uniprint\library\printer::printingTemplate('xxxxx', "3");

            }catch(\Throwable $e){
                $this->error($e->getMessage());
            }
        }
    }

}
